﻿using System;
using System.Collections.Generic;
using UploadClient.Framework;
using UploadClient.Interfaces;
using UploadClient.Interfaces.Upload;
using UploadClient.Utilitys;

namespace UploadClient.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        private bool UploadInProgress;
        private int _ActProgress;

        private string _StatusText;
        private List<UploadItem> files;

        public MainWindowViewModel(ICommunication communication)
        {
            Initial();

            OpenFileDialog = new DelegateCommand(OpenFile_Execute);
            Cancel = new DelegateCommand(Cancel_Execute, Cancel_CanExecute);
            Upload = new DelegateCommand(Upload_Execute, Upload_CanExecute);
            cancelClosingCommand = new DelegateCommand(CancelClosing_Execute);
            closingCommand = new DelegateCommand(Closing_Execute, Closing_CanExecute);
            Config = new FastConfigViewModel(communication);
            Communication = communication;

            App.CommandLineArgs += new CommandLineArgsHandler(CommandLineArgs);
        }

        public FastConfigViewModel Config { get; private set; }

        public string StatusText
        {
            get
            {
                return _StatusText;
            }
            set
            {
                _StatusText = value;
                OnPropertyChanged("StatusText");
            }
        }

        public int ActProgress
        {
            get
            {
                return _ActProgress;
            }
            set
            {
                _ActProgress = value;
                OnPropertyChanged("ActProgress");
            }
        }

        public DelegateCommand OpenFileDialog { get; private set; }
        public DelegateCommand Cancel { get; private set; }
        public DelegateCommand Upload { get; private set; }
        public DelegateCommand cancelClosingCommand { get; private set; }
        public DelegateCommand closingCommand { get; private set; }

        public void CommandLineArgs(string[] args)
        {
            if (args.Length > 0)
            {
                string result = string.Join(" ", args);
                files.Add(new UploadItem { Filepath = result });

                UpdateGuiFilecount();
            }
        }

        private void UpdateGuiFilecount()
        {
            StatusText = string.Format("{0}", files.Count);

            if (files.Count == 1)
                StatusText = string.Format(LocalizationManager.ResourceManager.GetString("UPL_0002"), files[0].Filename);
            else 
                StatusText = string.Format(LocalizationManager.ResourceManager.GetString("UPL_0003"), files.Count.ToString());
        }

        private void Initial()
        {
            files = new List<UploadItem>();
            UploadInProgress = false;
        }

        private void OpenFile_Execute(object param)
        {
            // ToDo: set upload type, current hard coded to ftpupload!


            var items = Communication.GetSelectedFiles();
            foreach (var item in items)
                files.Add(new UploadItem { Filepath = item, UploadType = new Guid("1443BC10-38D8-419a-99D5-18B2F11C21D0") });

            UpdateGuiFilecount();
        }

        private void Cancel_Execute(object param)
        {
            files.Clear();
        }

        private void Upload_Execute(object param)
        {
            Dictionary<Guid, List<UploadItem>> uploadQueues = new Dictionary<Guid, List<UploadItem>>();

            foreach (var item in files)
            {
                if (!uploadQueues.ContainsKey(item.UploadType))
                    uploadQueues.Add(item.UploadType, new List<UploadItem>());

                uploadQueues[item.UploadType].Add(item);
            }

            UploadInProgress = true;

            foreach (var item in uploadQueues)
            {
                using (IUploadProvider provider = Communication.GetUploadProvider(item.Key))
                {
                    if (provider != null)
                        while (item.Value.Count > 0)
                            provider.UploadFiles(item.Value.ToArray());
                    // ToDo: throw error / open config -> select "new" upload provider?
                }
            }

            UploadInProgress = false;
        }

        private bool Cancel_CanExecute(object param)
        {
            return files.Count > 0 && !UploadInProgress;
        }

        private bool Upload_CanExecute(object param)
        {
            return files.Count > 0 && !UploadInProgress;
        }

        private void CancelClosing_Execute(object param)
        {

            //Minimize the app 
            System.Windows.Application.Current.MainWindow.WindowState = System.Windows.WindowState.Minimized;
        }

        private bool Closing_CanExecute(object param)
        {
            return false;
        }

        private void Closing_Execute(object param)
        {

            //Do Nothing

        }

    }
}
